/*  
 * From. . .
 * MyImage.m	-- How to rotate an NXImage
 * 
 * This is a subclass of NXImage that supports rotation by overriding 
 * drawRepresentation:inRect:
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
 * fitness for any particular use.
 *
 * Written by Henry Krempel -- NeXT Developer Support
 *
 * Wed Apr 10 17:39:50 1991
 */
#import "RotImage.h"
#import <appkit/NXImage.h>
#import <appkit/View.h>
#import <dpsclient/wraps.h>
#import <math.h>
#import "wraps.h"


@implementation RotImage

 
- initFromFile:(const char *)fileName
{
	[super initFromFile:fileName];
	[self getSize:&origSize];
	rotSize.width=origSize.height;
	rotSize.height=origSize.width;
	currentSize=&origSize;
	flipped=0;
	return self;
}

- initFromSection:(const char *)imageName
{
	[super initFromSection:imageName];
	[self getSize:&origSize];
	rotSize.width=origSize.height;
	rotSize.height=origSize.width;
	currentSize=&origSize;
	flipped=0;
	return self;
}

/*
 *
 * drawRepresentation:inRect: override.  Any changes to the PostScript 
 * transformation matrix can take effect here. Compositing normally ignores
 * these changes,  but we are doing the transformation before the offscreen 
 * NXImage is rendered,  then compositing this into our visible window.
 *
 */
 
- (BOOL)drawRepresentation:(NXImageRep *)imageRep inRect:(const NXRect *)rect
{
    if (NXDrawingStatus == NX_DRAWING) {	// Don't do this when printing
	PSgsave();
	NXSetColor (NX_COLORCLEAR);
	NXRectFill (rect);
	PSgrestore();
    }
	
  /*
   * The wrap below translates and rotates to the appropriate starting point,
   * based on the rotation state (0 1 2 3) and the angle
   */
	 
    PSW_transformToFit(rotation, rotation, origSize.width, origSize.height);
	
    if (flippedHor) PSW_flipHor(origSize.width, origSize.height);
    if (flippedVer) PSW_flipVer(origSize.width, origSize.height);
            
    return [super drawRepresentation:imageRep inRect:rect];
}

/*
 * Set the rotation of the image.  This will be one of 4 values 0..3 for the 
 * four rotations we do here.  We only support these four since the size of
 * these rectangles is easy to calculate.
 *
 */
 
- setRotation:(int)anInt
{
	NXSize *size;

	rotation = anInt;
   
	size=&rotSize;
	
    	[self setSize:size];
    	[self recache];
	currentSize=size;

	return self;
}

- flip:(int)whichWay;
{
	if (whichWay == HORIZONTAL) flippedHor = (flippedHor+1) % 2;
	else flippedVer = (flippedVer+1) % 2;
	[self recache];
	return self;

}

@end

